package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.DictMapper;
import com.bwie.mapper.ProdImageMapper;
import com.bwie.pojo.Dict;
import com.bwie.pojo.Prod;
import com.bwie.pojo.ProdImage;
import com.bwie.service.ProdService;
import com.bwie.mapper.ProdMapper;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author 陈彤彤
 * @description 针对表【t_prod(商品表)】的数据库操作Service实现
 * @createDate 2025-04-28 10:02:20
 */
@Service
public class ProdServiceImpl extends ServiceImpl<ProdMapper, Prod>
        implements ProdService {

    @Autowired
    ProdMapper prodMapper;

    @Autowired
    DictMapper dictMapper;

    /*获取token*/
    @Autowired
    HttpServletRequest request;

    @Autowired
    ProdImageMapper prodImageMapper;

    /**
     * 商品列表
     * @param pageInfoVo
     * @return
     */
    @Override
    public R prodList(PageInfoVo pageInfoVo) {
        //构造分页参数
        Page<Prod> prodPage = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());

        Page<Prod> page = prodMapper.getProdList(prodPage, pageInfoVo.getKeyWord());

        //获取商品的图片列表
        for (Prod record : page.getRecords()) {
            List<ProdImage> list = prodImageMapper.selectList(
                    new QueryWrapper<ProdImage>()
                            .lambda()
                            .eq(ProdImage::getProdId, record.getProdId()));
            record.setProdImageList(list);
        }

        //购买数量默认设置为1
        for (Prod record : page.getRecords()) {
            record.setBuyNum(1);
        }

        return R.OK(page);
    }

    /**
     * 添加商品
     * @param prod
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public R addProd(Prod prod) {
        /*获取用户id*/
        String token = request.getHeader("token");
        //判断
        if (token == null) {
            return R.ERROR(500, "请先登录");
        }
        //解析token
        Claims claims = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        //获取用户id
        Integer userId = claims.get("id", Integer.class);

        /*判断商品是否重复*/
        Prod selectOne = prodMapper.selectOne(new QueryWrapper<Prod>().lambda().eq(Prod::getProdName, prod.getProdName()));
        if (selectOne != null) {
            return R.ERROR(404, "商品名称重复");
        }

        /*添加商品*/
        //将时间设置为当前时间
        prod.setCreateTime(new Date());
        //设置商品状态
        prod.setProdStatus(0);
        //根据token获取商户
        prod.setUserId(userId);
        prodMapper.insert(prod);

        /*添加商品图片*/
        if (prod.getProdImageList() != null && prod.getProdImageList().size() > 0) {
            //for循环
            for (ProdImage prodImage : prod.getProdImageList()) {
                //设置当前商品的id
                prodImage.setProdId(prod.getProdId());
                prodImageMapper.insert(prodImage);
            }
        }

        return R.OK();
    }

    /**
     * 字典表查询
     * @param typeId
     * @return
     */
    @Override
    public R dictList(Integer typeId) {
        List<Dict> dictList = dictMapper.selectList(
                new QueryWrapper<Dict>()
                        .lambda()
                        .eq(Dict::getDictType, typeId));
        return R.OK(dictList);
    }


    /**
     * 删除商品信息
     * @param prodId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R deleteProd(Integer prodId) {
        //删除商品图片
        prodImageMapper.delete(new QueryWrapper<ProdImage>().lambda().eq(ProdImage::getImageId,prodId));
        //判断商品是否存在
        Prod prod = prodMapper.selectById(prodId);
        if (prod == null){
            return R.ERROR(404,"商品信息不存在");
        }

        //删除
        prodMapper.deleteById(prodId);
        return R.OK();
    }

    /**
     * 修改商品信息
     * @param prod
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public R updateProd(Prod prod) {
        /*判断商品是否存在*/
        Prod prod1 = prodMapper.selectById(prod);
        if (prod1 == null){
            return R.ERROR(404,"商品不存在");
        }

        /*更新商品信息*/
        prodMapper.updateById(prod);

        /*删除所有商品图片*/
        prodImageMapper.delete(
                new QueryWrapper<ProdImage>()
                        .lambda()
                        .eq(ProdImage::getProdId,prod.getProdId()));

        /*添加商品图片*/
//        if (prod.getProdImageList() != null && prod.getProdImageList().size() > 0) {
//            //for循环
//            for (ProdImage prodImage : prod.getProdImageList()) {
//                //设置当前商品的id
//                prodImage.setProdId(prod.getProdId());
//                prodImageMapper.insert(prodImage);
//            }
//        }
        if (prod.getProdImageList() != null && prod.getProdImageList().size() > 0){
            //使用for循环
            for (ProdImage prodImage : prod.getProdImageList()) {
                //设置当前商品id
                prodImage.setProdId(prod.getProdId());
                prodImageMapper.insert(prodImage);
            }
        }
        return R.OK();
    }


}




